<?php

class Util_DB {
	
	
	
	static public function translate($class,$id,$nameField){
		if(is_string($class)){
			$class = new $class();
		}
		return $class->find($id)->current()->$nameField;
	}
	
	static public function quoteInto($template,$params){
		$db = Zend_Db_Table::getDefaultAdapter();
		if(is_array($params)){
			foreach($params as $p){
				$template = $db->quoteInto($template,$p,null,1);
			}
		} else {
			$template = $db->quoteInto($template,$params);
		}
		return $template;
	}
	
	/**
	 * Skrót do quoteInto
	 * @param sql_string $sql
	 * @param array|string|int $params
	 */
	static public function quote($sql,$params){
		return self::quoteInto($sql,$params);
	} 
		
	static public function insert($table,$data){
		$db = Zend_Db_Table::getDefaultAdapter();
		$db->insert($table, $data);
	}
	
	static public function table($class){
		// TODO czy da sie bez instancjonowania klasy?
		// dodać czy lpik istnieje???
		if(is_string($class)){
			if(class_exists($class)){
				$class = new $class(); 			
			} else die('U::table:1');
		}
		
		if($class instanceof My_Model){
			return $class->info('name');
		} else {
			die('U::table:2');	
		}
	}
	
	/**
	 * DUMP SQL
	 */

	static public function dump(){

		$mysql_host = 'localhost';
		$mysql_username = 'root';
		$mysql_password = '';
		$mysql_database = 'w6dni';
		
		mysql_select_db($mysql_database, mysql_connect($mysql_host, $mysql_username, $mysql_password));
		
		mysql_query('SET CHARACTER SET UTF8');
		
		self::_mysqldump($mysql_database);
		
      	$log_file = APPLICATION_PATH . '/../tmp/dump_'.date('Y-m-d_His');
      	fclose(fopen($log_file, 'w'));
      	$fh = fopen($log_file, 'a');
		fwrite($fh,self::_mysqldump($mysql_database));
		fclose($fh);
			
	}
	
	private static function _mysqldump($mysql_database){
	    $sql="show tables;";
	    $result= mysql_query($sql);
	    if( $result){
	    	$ret = '';
	        while( $row= mysql_fetch_row($result)){
	            $ret.= self::_mysqldump_table_structure($row[0]);
	            $ret.= self::_mysqldump_table_data($row[0]);
	        }
	        return $ret;
	    } else {
	        return "/* no tables in $mysql_database */\n";
	    }
	    mysql_free_result($result);
	}
		
	private static function _mysqldump_table_structure($table){
		$ret = '';
		$ret.= "/* Table structure for table `$table` */\n";
		$ret.= "DROP TABLE IF EXISTS `$table`;\n\n";
		
        $sql="show create table `$table`; ";
        $result=mysql_query($sql);
        if( $result){
            if($row= mysql_fetch_assoc($result)){
                $ret.= $row['Create Table'].";\n\n";
            }
        }
        mysql_free_result($result);
    	return $ret;
	}
		
	private static function _mysqldump_table_data($table){
	    $sql="select * from `$table`;";
	 	$ret = '';
	    $result=mysql_query($sql);
	 
	    if( $result){
	 
	        $num_rows= mysql_num_rows($result);
	        $num_fields= mysql_num_fields($result);
	        if( $num_rows> 0){
	            $ret.= "/* dumping data for table `$table` */\n";
	            
	            $field_type=array();
	            $i=0;
	            while( $i <$num_fields){
	                $meta= mysql_fetch_field($result, $i);
	                array_push($field_type, $meta->type);
	                $i++;
	            }
	            //print_r( $field_type);
	            $ret.= "insert into `$table` values\n";
	            $index=0;
	            while( $row= mysql_fetch_row($result)){
	                $ret.= "(";
	                for( $i=0; $i <$num_fields; $i++){
	                    if( is_null( $row[$i]))
	                        $ret.= "null";
	                    else {
	                        switch( $field_type[$i]){
	                            case 'int':
	                                $ret.= $row[$i];
	                                break;
	                            case 'string':
	                            case 'blob' :
	                            default:
	                                $ret.= "'".mysql_real_escape_string($row[$i])."'";
	                        }
	                    }
	                    if( $i <$num_fields-1)
	                        $ret.= ",";
	                }
	                $ret.= ")";
	                if( $index <$num_rows-1)
	                    $ret.= ",";
	                else
	                    $ret.= ";";
	                $ret.= "\n";
	                $index++;
	            }
	        }
	    }
	    mysql_free_result($result);
	    $ret.= "\n";
	    return $ret;
	}
	
	
}